package com.fz.song.service;


import com.fz.song.enums.ExceptionEnum;
import com.fz.song.enums.UserEnum;
import com.fz.song.exception.CustomException;
import com.fz.song.mapping.SysUserMapper;
import com.fz.song.pojo.SysUser;
import com.fz.song.util.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public SysUserMapper sysUserMapper;


    public Map<String, Object> userLogin(String loginname, String password) throws CustomException {
        List<SysUser> list = sysUserMapper.selectByName(loginname);
        if (list == null || list.size() == 0) {
            throw new CustomException(ExceptionEnum.USER_NOT_EXISTS.getCode(), ExceptionEnum.USER_NOT_EXISTS.getMessage());
        }
        if (list != null && list.size() > 1) {
            throw new CustomException(ExceptionEnum.USER_NOT_ONLY.getCode(), ExceptionEnum.USER_NOT_ONLY.getMessage());
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        SysUser sysUser = list.get(0);
        Integer state = sysUser.getState();
        if (state == UserEnum.USER_LOCK.getCode()) {
            map.put("status", false);
            map.put("message", UserEnum.USER_LOCK.getValue());
            return map;
        } else if (state == UserEnum.USER_QUIT.getCode()) {
            map.put("status", false);
            map.put("message", UserEnum.USER_QUIT.getValue());
            return map;
        } else if (state == UserEnum.USER_NORMAL.getCode()) {
            String password_db = sysUser.getPassword();
            if (MD5Util.MD5(password).equalsIgnoreCase(password_db)) {
                map.put("status", true);
                map.put("message", "登录成功");
                map.put("obj", sysUser);
                return map;
            } else {
                map.put("status", false);
                map.put("message", "密码错误!");
                return map;
            }
        } else {
            map.put("status", false);
            map.put("message", "未知错误!");
            return map;
        }
    }

    public PageInfo<SysUser> queryUserByPage(Map<String, Object> params, int pageNo, int pageSize) {
        pageNo = pageNo == 0 ? 1 : pageNo;
        pageSize = pageSize == 0 ? 10 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<SysUser> list = sysUserMapper.getUserList(params);
        PageInfo<SysUser> page = new PageInfo<SysUser>(list);
        return page;
    }

    public SysUser getUserByLoginName(String loginname) {
        List<SysUser> sysUsers = sysUserMapper.selectByName(loginname);
        if (sysUsers == null || sysUsers.size() == 0) {
            return null;
        }
        return sysUsers.get(0);
    }

    public SysUser getUserById(int sysid) {
        return sysUserMapper.selectByPrimaryKey(sysid);
    }

    public int saveUser(SysUser sysUser) {
        Integer sysid = sysUser.getSysid();

        if(sysid==0){
             return  sysUserMapper.insertSelective(sysUser);
        }
        return  sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    public int changeStatus(int sysid, int status) {
        return sysUserMapper.changeStatus(sysid,status);
    }
}
